package org.easy.db;

import lombok.Data;
import org.easy.db.generator.*;

import javax.sql.DataSource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 数据库工厂类
 * @Author: wxl
 * @CreateTime: 2022/10/28 9:38
 */
@Data
public class DataBaseFactory implements IDataBaseOperation, DataBaseConnectTemplate, IGenerator {

    private boolean connectStatus;

    private DataBaseInfo dataBase;

    private DataSource dataSource;

    private IGenerator generator;

    private IDataBaseOperation iDataBaseOperation;

    private DataBaseConnectTemplate dataBaseConnectTemplate;

    public DataBaseFactory (DataBaseInfo dataBase) {
        this.dataBase = dataBase;
        this.generator = new Generator();
        this.dataSource = dataBase.getDataSource();
        this.dataBaseConnectTemplate = new ConnectTemplate(this.dataSource);
        this.iDataBaseOperation = new DataBaseOperation(this.dataBaseConnectTemplate);
    }

    /**
     * 根据数据库名称查找所有的表
     *
     * @param dbName 数据库名
     * @return List<String>
     */
    @Override
    public Map<String,List<String>> queryTableNamesByDbName(String dbName) {
        return this.iDataBaseOperation.queryTableNamesByDbName(dbName);
    }

    /**
     * 创建表结构
     *
     * @param sql 表结构语句
     * @return boolean
     */
    @Override
    public boolean createTable(String sql) {
        return this.iDataBaseOperation.createTable(sql);
    }

    /**
     * 查询
     *
     * @param sql 查询语句
     * @return List<Map < String, Object>>
     */
    @Override
    public List<Map<String, Object>> query(String sql) {
        return this.iDataBaseOperation.query(sql);
    }

    /**
     * 更新
     *
     * @param sql 更新语句
     * @return int
     */
    @Override
    public int update(String sql) {
        return this.iDataBaseOperation.update(sql);
    }

    /**
     * 删除
     *
     * @param sql 删除语句
     * @return int
     */
    @Override
    public int delete(String sql) {
        return this.iDataBaseOperation.delete(sql);
    }

    /**
     * alter
     *
     * @param sql alter语句
     */
    @Override
    public void alter(String sql) {
        this.iDataBaseOperation.alter(sql);
    }

    /**
     * create
     *
     * @param sql alter语句
     */
    @Override
    public void create(String sql) {
        this.iDataBaseOperation.create(sql);
    }

    /**
     * 根据表名查询表字段
     *
     * @param tableName 表名
     * @return List<ColumnDefinition>
     */
    public List<ColumnDefinition> columnConverter(String tableName) {
        Type type = getDataBase().getType();
        String sql = type.tableColumnSql(tableName);
        List<Map<String, Object>> result = query(sql);
        if (!result.isEmpty()) {
            return result.stream().map(ColumnDefinition::new).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }


    /**
     * 连接数据库
     */
    @Override
    public void connect() {
        this.dataBaseConnectTemplate.connect();
    }

    /**
     * 连接数据库
     *
     * @param dataSource 数据库配
     */
    @Override
    public void connect(DataSource dataSource) {
        this.dataBaseConnectTemplate.connect(dataSource);
        setConnectStatus(true);
    }

    public boolean getConnectStatus () {
        return this.connectStatus;
    }

    /**
     * 自动生成代码
     *
     * @param builder 配置信息
     */
    @Override
    public void generator(GlobalConfigBuilder builder) {
        builder.setType(getDataBase().getType());
        TableDefinition tableDefinition = builder.getTable();
        List<ColumnDefinition> columnDefinitions = columnConverter(builder.getTableName());
        tableDefinition.setColumnDefinitionList(columnDefinitions);
        // TODO 生成的代码存放的路径需要制定
        this.generator.generator(builder);
    }
}
